package com.zhai.cloud.home.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhai.cloud.common.domain.Result;
import com.zhai.cloud.common.util.StringUtil;
import com.zhai.cloud.home.client.UserClient;
import com.zhai.cloud.home.constant.HomeConstant;
import com.zhai.cloud.home.domain.JobIn;
import com.zhai.cloud.home.domain.User;
import com.zhai.cloud.home.service.HomeJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @package: com.zhai.cloud.home.controller
 * @project-name: zhai-cloud
 * @description: 首页 controller
 * @author: Created by 翟凤玺
 * @create-datetime: 2018/5/17 0017 19:09
 */
@Api(value = "首页")
@RestController
public class HomeController {

    @Autowired
    UserClient userClient;
    @Autowired
    HomeJobService homeJobService;

    @ApiOperation(value="查询demo信息", notes="根据条件查询demo信息")
    @ApiImplicitParam(name = "in", dataType = "DemoFindIn", value = "demo查询的入参", required = true, paramType = "body")
    @PostMapping("/home")
    @HystrixCommand(fallbackMethod = "findByIdFeignHystrix")
    public Result findByIdFeign(@RequestBody @Validated User user) {
        return userClient.findByUser(user);
    }

    public Result findByIdFeignHystrix(@RequestBody @Validated User user) {
        return Result.failure("-1", "熔断");
    }

    @ApiOperation(value="定时任务", notes="定时任务")
    @ApiImplicitParam(name = "in", dataType = "JobIn", value = "定时任务 入参", required = true, paramType = "body")
    @PostMapping("/job")
    public Result job(@RequestBody @Validated JobIn in) {
        if (HomeConstant.JOB_STATE_START.equals(in.getState()) || HomeConstant.JOB_STATE_RESTART.equals(in.getState())) {
            homeJobService.start(in.getJobCode(), in.getCron(), in.getState());
        }
        if (HomeConstant.JOB_STATE_STOP.equals(in.getState())) {
            homeJobService.stop(in.getJobCode());
        }
        return Result.success();
    }

}
